unit Bubble1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    ScrollBar1: TScrollBar;
    Edit1: TEdit;
    procedure ScrollBar1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Start1;
  end;

var
  Form1: TForm1;
  a:boolean;
  run, step, N : integer;
  mass : array [0..100] of integer;
Const
  W=50;
  H=10;
  x=50;
  y=10;

implementation

{$R *.dfm}

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
  Timer1.Interval:=ScrollBar1.Position*10;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
  randomize;
  Form1.Canvas.Brush.Color:=clWhite;
  Form1.Canvas.Pen.Color:=clWhite;
  Form1.Canvas.Rectangle(Form1.ClientRect);
  step:=0;
  run:=0;
  Timer1.Enabled:=True;
  N:=StrToInt(Edit1.Text);
  for i :=0 to N-1 do
    mass[i]:=random(N+30)+10;

end;

procedure TForm1.Start1;
var i : integer;
begin
  for i := 0 to N-1 do
  begin
    Form1.Canvas.Brush.Color:=clWhite;
    Form1.Canvas.Pen.Color:=clWhite;
    Form1.Canvas.Rectangle(x+W*i, y, x+W*(i+1), y+h*(N+40));

    Form1.Canvas.Brush.Color:=clAqua;
    Form1.Canvas.Pen.Color:=clBlue;
    Form1.Canvas.Rectangle(x+W*i, y+h*(N+40)-h*mass[i], x+W*(i+1), y+h*(N+40));
    Form1.Canvas.Rectangle(x+W*(i+1), y+h*(N+40)-h*mass[i+1], x+W*(i+2), y+h*(N+40));

    if (i=step) and (i+1=step+1) then
    begin
      Form1.Canvas.Brush.Color:=clWhite;
      Form1.Canvas.Pen.Color:=clWhite;
      Form1.Canvas.Rectangle(x+W*i, y, x+W*(i+1), y+h*(N+40));
      Form1.Canvas.Rectangle(x+W*(i+1), y, x+W*(i+2), y+h*(N+40));

      Form1.Canvas.Brush.Color:=clYellow;
      Form1.Canvas.Pen.Color:=clRed;
      Form1.Canvas.Rectangle(x+W*i, y+h*(N+40)-h*mass[i], x+W*(i+1), y+h*(N+40));
      Form1.Canvas.Rectangle(x+W*(i+1), y+h*(N+40)-h*mass[i+1], x+W*(i+2), y+h*(N+40));
    end;

    if (i=step+1) and (i+1=step+2) then
    begin
      Form1.Canvas.Brush.Color:=clWhite;
      Form1.Canvas.Pen.Color:=clWhite;
      Form1.Canvas.Rectangle(x+W*(i), y, x+W*(i+1), y+h*(N+40));

      Form1.Canvas.Brush.Color:=clYellow;
      Form1.Canvas.Pen.Color:=clRed;
      Form1.Canvas.Rectangle(x+W*i, y+h*(N+40)-h*mass[i], x+W*(i+1), y+h*(N+40));
    end;


  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var i : integer;
begin
  if (step=0) then
    a:=true;

  if(mass[step]>mass[step+1]) then
  begin
    mass[step]:=mass[step]+mass[step+1];
    mass[step+1]:=mass[step]-mass[step+1];
    mass[step]:=mass[step]-mass[step+1];
    a:=false;
  end;
  Start1;
  step:=step+1;

  if(step=N-(run+1)) then
  begin
    step:=0;
    run:=run+1;
    if (a) or (run=N-1) then
    begin
      step:=50;
      Start1;
      Timer1.Enabled:=False;
    end;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled:=false;
end;

end.
